﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace BySoft.Web.Site.ComPage
{
    public class GridViewTemplate : ITemplate
    {
        #region 参数
        private DataControlRowType templateType;     //区分列的类型        
        private string headName;                     //表头名称        
        private List<Control> controlList;           //控件列表，支持同一列添加多个控件
        private string strSeparator = "^";           //绑定字段与格式之间的分隔符，绑定字段与格式通过传入控件的属性传入。        
        private char strSeparatorItem = '/';         //绑定字段之间的分隔符，用于支持多字段绑定。        
        public DataControlRowType TemplateType        
        {            
            get 
            { return templateType; }            
            set { templateType = value; }        
        }        
        public string HeadName        
        {            
            get { return headName; }            
            set { headName = value; }        
        }        
        public List<Control> ControlList        
        {            
            get { return controlList; }            
            set { controlList = value; }        
        }        
        
        public string StrSeparator        
        {            
            get { return strSeparator; }            
            set { strSeparator = value; }        
        }        
        public char StrSeparatorItem        
        {            
            get { return strSeparatorItem; }            
            set { strSeparatorItem = value; }        
        }        
        #endregion

        #region 构造        
        public GridViewTemplate()        
        {        
        }        
        
        public GridViewTemplate(DataControlRowType templateType, string headName)   
        {            
            this.templateType = templateType;           
            this.headName = headName;        
        }        
        public GridViewTemplate(DataControlRowType templateType, List<Control> controlList)
        {            
            this.templateType = templateType;            
            this.controlList = controlList;        
        }        
        #endregion

        void ITemplate.InstantiateIn(Control container) 
        { 
            switch (templateType) 
            { 
                case DataControlRowType.Header:                    
                    Literal lc = new Literal(); 
                    lc.Text = headName; 
                    container.Controls.Add(lc); 
                    break; 
                case DataControlRowType.DataRow:                    
                    foreach (Control control in controlList) 
                    { 
                        string strControlType = control.GetType().ToString().Trim(); 
                        switch (strControlType.Substring(strControlType.LastIndexOf('.') + 1, strControlType.Length - strControlType.LastIndexOf('.') - 1).Trim()) 
                        { 
                            case "Label":                                
                                AddControl(container, control, AddLabel); 
                                break; 
                            case "TextBox":                                
                                //AddControl(container, control, AddTextBox); 
                                break; 
                            case "DropDownList":                                
                                //AddControl(container, control, AddDropDownList); 
                                break; 
                            case "Button":                                
                                //AddControl(container, control, AddButton); 
                                break; 
                            default:                                
                                break; 
                        } 
                    } 
                    break; 
                default:                    
                    break; 
            }
        }

        #region 委托        
        /// <summary>        
        /// 添加控件--委托        
        /// </summary>        
        /// <param name="container"></param>        
        /// <param name="control"></param>        
        private delegate void AddControlDelegate(Control container, Control control);        
        /// <summary>        
        /// 使用委托添加控件        
        /// </summary>        
        /// <param name="container"></param>        
        /// <param name="control">控件</param>        
        /// <param name="addControlDelegate">添加控件方法</param>        
        private void AddControl(Control container, Control control, AddControlDelegate addControlDelegate)        
        {            
            addControlDelegate(container, control);        
        }        
        #endregion

        #region Label        
        private void AddLabel(Control container, Control control)        
        {            
            Label lblTemp = new Label();            
            lblTemp.ID = control.ID;            
            lblTemp.Text = ((Label)control).Text;            
            lblTemp.ToolTip = ((Label)control).ToolTip;            
            lblTemp.Visible = ((Label)control).Visible; 
            lblTemp.ApplyStyleSheetSkin(((Label)control).Page);
            lblTemp.ControlStyle.CopyFrom(((Label)control).ControlStyle); 
            lblTemp.CopyBaseAttributes((Label)control);            
            lblTemp.DataBinding += new EventHandler(Label_DataBinding);
            container.Controls.Add(lblTemp);        
        }        
        
        private void Label_DataBinding(object sender, EventArgs e)        
        {            
            Label lblTemp = (Label)sender;            
            GridViewRow row = (GridViewRow)lblTemp.NamingContainer;            
            string strTempText = lblTemp.Text.Trim();            
            if ((!String.IsNullOrEmpty(strTempText)) && strTempText.Contains(strSeparator))
            {                
                lblTemp.Text = "";                
                string strBindStr = strTempText.Substring(0, strTempText.LastIndexOf(strSeparator));                
                string strBindStrFomat = strTempText.Substring(strTempText.LastIndexOf(strSeparator) + 1, strTempText.Length - strTempText.LastIndexOf(strSeparator) - 1);                
                string[] strBindStrSubs = strBindStr.Split(strSeparatorItem);
                string[] strBindStrFomats = strBindStrFomat.Split(strSeparatorItem); 
                if (strBindStrSubs.Length == strBindStrFomats.Length)  
                {                    
                    for (int i = 0; i < strBindStrSubs.Length; i++)  
                    {                        
                        lblTemp.Text += DataBinder.Eval(row.DataItem, strBindStrSubs[i], strBindStrFomats[i]).ToString();                   
                    }                
                }                
                else                
                {                    
                    lblTemp.Text = "绑定项的个数与设置绑定项格式的字段个数不同";                
                }            
            }            
            string strTempToolTip = lblTemp.ToolTip.Trim();            
            if ((!String.IsNullOrEmpty(strTempToolTip)) && strTempToolTip.Contains(strSeparator))            
            {                
                lblTemp.ToolTip = "";                
                string strBindStr = strTempToolTip.Substring(0, strTempToolTip.LastIndexOf(strSeparator));                
                string strBindStrFomat = strTempToolTip.Substring(strTempToolTip.LastIndexOf(strSeparator) + 1, strTempToolTip.Length - strTempToolTip.LastIndexOf(strSeparator) - 1);                
                string[] strBindStrSubs = strBindStr.Split(strSeparatorItem);  
                string[] strBindStrFomats = strBindStrFomat.Split(strSeparatorItem);  
                if (strBindStrSubs.Length == strBindStrFomats.Length)  
                {                    
                    for (int i = 0; i < strBindStrSubs.Length; i++) 
                    {                       
                        lblTemp.ToolTip += DataBinder.Eval(row.DataItem, strBindStrSubs[i], strBindStrFomats[i]).ToString();                   
                    }                
                }                
                else                
                {                    
                    lblTemp.ToolTip = "绑定项的个数与设置绑定项格式的字段个数不同";                
                }           
            }        
        }        
        #endregion

        
    }
}